#!/bin/bash
# Special thank to Shiroko, he give me the insight of coding in proper way

################################# Here we import our tools
source /opt/bashimport/sbs-utils.bashimport
source /opt/bashimport/sbs-build-helper.bashimport

if [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="-amd64"
ARCH_STORE_PATH="amd64-store"
elif [ "$1" = "arm64" ] || [ "$1" = "arm" ];then
ARCH="arm64"
ARCH_ANOTHERWAY="-aarch64"
ARCH_STORE_PATH="arm64-store"
elif [ "$1" = "loong64" ];then
ARCH="loong64"
ARCH_ANOTHERWAY="-loong64"
ARCH_STORE_PATH="loong64-store"
else
log.error "Err:Invalid Architecture"
exit 1
fi

################################# Here we set the configs
REALPATH=`realpath $0`
HERE_PATH=`dirname $REALPATH`
REPO_PATH="${REPO_PATH:-/root/store-dir/}"
WORKDIR=`mktemp -d`

PKG_NAME="firefox-spark"
APP_NAME="Firefox 火狐浏览器"
SOURCE_LIST_FILE="firefox.list"
GITHUB_REPO=""
VERSION="" #Here we use other way to get version so do not write here
TAGS="ubuntu;deepin;uos;debian"
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="Firefox是一个自由和开放源代码的网页浏览器\n一款开源自由的浏览器，现在由星火自动维护系统（测试版）维护中..." # This should be written as json format
MAINTAINER="shenmo<shenmo@spark-app.store>"
HOMEPAGE="https://www.mozilla.org/"

STORE_APP_PATH="${REPO_PATH}/${ARCH_STORE_PATH}/${CATAGORY}/${PKG_NAME}/"
################################# Fill the variables below only if you use write_control function

SECTION="web"
PRIORITY="optional"
DEPENDS="libdbus-glib-1-2"
DESCRIPTION_SHORT="一款开源自由的浏览器，现在由星火自动维护系统（测试版）维护中..."
DESCRIPTION_LONG="一款开源自由的浏览器，现在由星火自动维护系统（测试版）维护中..."
PROVIDES="x-www-browser,gnome-www-browser"
REPLACES="firefox"
RECOMMENDS="speech-dispatcher"



function prepare(){
#    aptss_get_update
    PKG_VER_APTSS=`get_version_from_source_list_file ${PKG_NAME}:${ARCH} /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore${ARCH_ANOTHERWAY}.list`
    PKG_VER_UPSTREAM=$(curl -fI 'https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=zh-CN' | grep -o 'firefox-[0-9.]\+[0-9]' | sed 's/.*firefox-//')
    compare_version ${PKG_VER_APTSS} ${PKG_VER_UPSTREAM}
    # Compare version will also give the value to $VERSION
    # Now we should have the $VERSION 
    if [ -e $VERSION ];then
    log.error "VERSION is empty. This should not happen! exit."
    fi
}




function build(){



pushd $WORKDIR

init_package_directory
write_control
write_info


Download_Name="fflatest.tar.bz2"
Download_Url="https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=zh-CN"
aria2c -d "opt/apps/firefox-spark/files" "$Download_Url" -o "$Download_Name" --summary-interval=1
tar -xvf "opt/apps/firefox-spark/files"/"$Download_Name" -C "opt/apps/firefox-spark/files"
rm "opt/apps/firefox-spark/files"/"$Download_Name"
mkdir -p opt/apps/firefox-spark/files/firefox/distribution/extensions/

mkdir temp

pushd temp
aptss download firefox-locale-*
for i in $(echo *.deb) ;do 
    dpkg -X $i ./
done
rm -fv *.deb
cp -vr usr/lib/firefox-addons/extensions/* ../opt/apps/firefox-spark/files/firefox/distribution/extensions/

popd
rm -vfr temp

cat << EOFFFFFF > opt/apps/firefox-spark/files/firefox/distribution/policies.json
{
    "policies":
    {
        "DisableAppUpdate": true
    }
}
EOFFFFFF

cat << EOFFFFFF > opt/apps/firefox-spark/files/firefox/distribution/distribution.ini
[Global]
id=Spark Store
version=2.0
about=Mozilla Firefox for Spark Store

[Preferences]
app.distributor="Project Spark"
intl.locale.requested=""
app.distributor.channel="Spark Store"
mozilla.partner.id="Spark Store"
EOFFFFFF

dpkg-deb -Z xz -b . ${STORE_APP_PATH}/${PKG_NAME}_${VERSION}_${ARCH}.deb


popd

}


prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR
